package com.baidu.common.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Security {
    private static byte[] defaultKey = { 01, 23, 45, 67, 76, 54, 32, 10 };
    
    public static byte[] encrypt(byte[] plainText) {
        return encrypt(plainText, defaultKey, "DES");
    }
    
    public static byte[] encrypt(byte[] plainText, byte[] secretKey, String algorithm) {
        if (plainText == null || secretKey == null) {
            return null;
        }
        try {
            SecretKey desKey = new SecretKeySpec(secretKey, algorithm);
            Cipher cipher = Cipher.getInstance(algorithm + "/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, desKey);
            byte[] cipherBytes = cipher.doFinal(plainText);
            return cipherBytes;
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String encrypt(String plainText) {
        try {
            if (plainText == null) {
                return null;
            }
            byte[] cipher = encrypt(plainText.getBytes());
            return Base64.encode(cipher);
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String decrypt(String cipherText) {
        try {
            if (cipherText == null) {
                return null;
            }
            return new String(decrypt(Base64.decode(cipherText)));
        } catch (Exception e) {
            return null;
        }
    }
    
    public static byte[] decrypt(byte[] cipherText) {
        return decrypt(cipherText, defaultKey);
    }
    
    public static byte[] decrypt(byte[] cipherText, byte[] secretKey) {
        if (cipherText == null || secretKey == null) {
            return null;
        }
        try {
            SecretKey desKey = new SecretKeySpec(secretKey, "DES");
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, desKey);
            byte[] plainBytes = cipher.doFinal(cipherText);
            return plainBytes;
        } catch (Exception e) {
            return null;
        }
    }
    
    public static byte[] genCroptyKey(byte[] encodeKeyA, String randomStrB) {
        if (encodeKeyA == null) {
            return null;
        }
        byte[] A = encodeKeyA;
        byte[] B = new byte[24];
        byte[] C = randomStrB.getBytes();
        int alen = A.length;
        int clen = C.length;
        if (alen != 24 || (clen < 8 || clen > 20))
            return null;
        int demension = alen - clen;
        for (int i = 0; i < C.length; i++) {
            B[i] = C[i];
        }
        
        int piont = 1;
        while (demension > 0) {
            if (demension > clen) {
                for (int i = 0; i < clen; i++) {
                    B[clen * piont + i] = C[i];
                }
                piont++;
                
            } else {
                for (int i = 0; i < demension; i++) {
                    B[clen * piont + i] = C[i];
                }
            }
            demension = demension - clen;
            
        }
        byte[] result = new byte[24];
        for (int i = 0; i < alen; i++) {
            switch ((i + 1) % 3) {
                case 0:
                    result[i] = (byte) (A[i] ^ B[i]);
                    break;
                case 1:
                    result[i] = (byte) (A[i] & B[i]);
                    break;
                case 2:
                    result[i] = (byte) (A[i] | B[i]);
                    break;
            }
            
        }
        return result;
    }
}
